package com.xmg.boot.demo.bootlesson1.user.job;

import com.xmg.boot.demo.bootlesson1.user.entity.pojo.Student;
import com.xmg.boot.demo.bootlesson1.user.entity.pojo.User;
import com.xmg.boot.demo.bootlesson1.user.mapper.StudentMapper;
import com.xmg.boot.demo.bootlesson1.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;

/**
 * @Author: ma
 * @CreateTime: 2023-11-23
 * @Description: 测试JOB功能
 * @Version: 1.0
 */
//@Component
//@EnableScheduling  //开启定时任务
//@EnableAsync //开启多线程
public class AnnotationJob {

    @Autowired
    private UserService userService;

    // [秒] [分] [小时] [日] [月] [周]

    @Scheduled(cron = "0/30 * * * * ?")
    public void testSecond(){
        System.out.println("每3秒执行一次调度："+new Date());
    }

    @Async
    @Scheduled(cron = "0 0/1 * * * ?")
    public void ActionSql(){
        User user=new User();
        user.setUserName("定时姓名");
        user.setUserGender("定时性别");
        user.setUserAge(new Random().nextInt(100));
        userService.save(user);
        System.out.println("每1分钟执行插入sql");
    }
}
